package cn.edu.hbvtc.qms.web.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import cn.edu.hbvtc.qms.domain.Criteria;
import cn.edu.hbvtc.qms.service.IEvaluationService;
import cn.edu.hbvtc.qms.web.struts.form.CriteriaActionForm;

public class CriteriaAction extends AbstractBaseDispatchAction {

	private IEvaluationService evaluationService = null;

	public IEvaluationService getEvaluationService() {
		return evaluationService;
	}

	public void setEvaluationService(IEvaluationService evaluationService) {
		this.evaluationService = evaluationService;
	}

	// 跳转到评价标准表列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		List<Criteria> criteriaList = evaluationService.findCriterias();
		request.setAttribute("criteriaList", criteriaList);

		return mapping.findForward("list");
	}

	// 跳转到评价标准表添加页面
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("add");
	}

	// 完成添加功能
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CriteriaActionForm caf = (CriteriaActionForm) form;

		if (caf.getName() == null || "".equals(caf.getName())) {
			request.setAttribute("nameError", "请输入标准名称！");

			return mapping.findForward("add");
		}
		
		if (caf.getDescription() == null || "".equals(caf.getDescription())) {
			request.setAttribute("descriptionError", "请输入描述！");

			return mapping.findForward("add");
		}
		
		if (caf.getPurpose().equals("0")) {
			request.setAttribute("purposeError", "请选择评价用途！");

			return mapping.findForward("add");
		}
		
		if (caf.getEnabled() == 0) {
			request.setAttribute("enabledError", "请设置有效状态！");

			return mapping.findForward("add");			
		}
		
		Criteria criteria = new Criteria();
		PropertyUtils.copyProperties(criteria, caf);
		evaluationService.saveCriteria(criteria);

		return mapping.findForward("addSave");
	}

	// 跳转到评价标准表修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		CriteriaActionForm caf = (CriteriaActionForm) form;
		Criteria criteria = evaluationService.findCriteriaById(caf.getId());
		request.setAttribute("criteria", criteria);

		return mapping.findForward("update");
	}

	// 完成修改保存功能
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CriteriaActionForm caf = (CriteriaActionForm) form;

		if (caf.getName() == null || "".equals(caf.getName())) {
			request.setAttribute("nameError", "请输入标准名称！");

			Criteria criteria = evaluationService.findCriteriaById(caf.getId());
			request.setAttribute("criteria", criteria);

			return mapping.findForward("update");
		}
		if (caf.getDescription() == null || "".equals(caf.getDescription())) {
			request.setAttribute("descriptionError", "请输入描述！");

			Criteria criteria = evaluationService.findCriteriaById(caf.getId());
			request.setAttribute("criteria", criteria);

			return mapping.findForward("update");
		}
		if (caf.getPurpose().equals("0")) {
			request.setAttribute("purposeError", "请输入评价用途！");

			Criteria criteria = evaluationService.findCriteriaById(caf.getId());
			request.setAttribute("criteria", criteria);

			return mapping.findForward("update");
		}

		if (caf.getEnabled() == 0) {
			request.setAttribute("enabledError", "请设置有效状态！");

			Criteria criteria = evaluationService.findCriteriaById(caf.getId());
			request.setAttribute("criteria", criteria);

			return mapping.findForward("update");
		}
		
		Criteria criteria = evaluationService.findCriteriaById(caf.getId());
		PropertyUtils.copyProperties(criteria, caf);

		evaluationService.updateCriteria(criteria);

		return mapping.findForward("updateSave");
	}

	// 完成评价标准表删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		CriteriaActionForm caf = (CriteriaActionForm) form;
		evaluationService.deleteCriteriaById(caf.getId());

		return mapping.findForward("delete");
	}
}
